package com.travel.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travel.common.utils.JwtUtil;
import com.travel.mapper.AdminUserMapper;
import com.travel.pojo.dto.UserDTO;
import com.travel.pojo.entity.AdminUser;
import com.travel.service.IAdminUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
@CrossOrigin
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {

    private final AdminUserMapper adminUserMapper;

    @Override
    public AdminUser login(UserDTO userDTO) {
        String name = userDTO.getUsername();
        String password = userDTO.getPassword();
        AdminUser adminUser = lambdaQuery().eq(AdminUser::getName, name)
                .eq(AdminUser::getPassword, password).one();
        Map<String, Object> map = new HashMap<>();
        if (adminUser != null) {
            map.put("typ", "JWT");
            map.put("alg", "HS256");
            String token = JwtUtil.generatorJWT(map);
            adminUser.setToken(token);
        }

        return adminUser;

    }
}
